package com.telit.firecommand.controller;

import com.telit.common.entity.DataReturn;
import com.telit.firecommand.dto.SpreadDTO;
import com.telit.firecommand.entity.NowWeather;
import com.telit.firecommand.service.WeatherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther zhangwei
 * @Describe 天气控制层
 */
@RestController
@RequestMapping("/weather")
@Api(tags = "天气的控制层")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @GetMapping(value = "/now", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取实时天气信息")
    public DataReturn<NowWeather> getNowWeather(@RequestParam("location") String location) {
        return DataReturn.ok(weatherService.getNowWeather(location));
    }

    @GetMapping(value = "/today", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取当天最高最低气温")
    public DataReturn getTodayWeather(@RequestParam("location") String location,
                                      @RequestParam(value = "start", defaultValue = "0") Integer start,
                                      @RequestParam(value = "day", defaultValue = "5") Integer day) {
        return DataReturn.ok(weatherService.getOneDayWeather(location, start, day));
    }

    @GetMapping(value = "/daily", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取五天的天气数据")
    public DataReturn getDailyWeather(@RequestParam("location") String location,
                                      @RequestParam(value = "start", defaultValue = "0") Integer start,
                                      @RequestParam(value = "day", defaultValue = "5") Integer day) {
        return DataReturn.ok(weatherService.getDailyWeatherString(location, start, day));
    }

    @GetMapping(value = "/hourly", produces = "application/json;charset=UTF-8")
    @ApiOperation("24小时逐小时天气预报")
    public DataReturn getHourlyWeather(@RequestParam("location") String location) {
        return DataReturn.ok(weatherService.getHourlyWeather(location));
    }

    @GetMapping(value = "/getSpreadInfo", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取蔓延数据")
    public DataReturn getSpreadInfo(@RequestParam("location") String location) {
        NowWeather nowWeather = weatherService.getNowWeather(location);
        Double spreadRate = weatherService.getSpreadRate(nowWeather);
        if (nowWeather != null) {
            SpreadDTO spreadDTO = new SpreadDTO(nowWeather, spreadRate);
            return DataReturn.ok(spreadDTO);
        }
        return DataReturn.ok();
    }

}
